package com.example.myutil;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * 
 */
public class ObjectUtil {

	private ObjectUtil() {
	}

	private static final Logger LOG = LoggerFactory.getLogger(ObjectUtil.class);

	/**
	 * <p>去掉实例对象property中字符串中的空格</p>
	 * {@link https://stackoverflow.com/questions/21894187/is-there-a-common-java-method-to-trim-every-string-in-an-object-graph}
	 * 
	 * @param object
	 */
	public static void trimProperty(Object object) {
		if (object != null) {
			Class<? extends Object> c = object.getClass();
			try {
				// Introspector usage to pick the getters conveniently thereby
				// excluding the Object getters
				for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(c, Object.class)
						.getPropertyDescriptors()) {
					Method method = propertyDescriptor.getReadMethod();
					String name = method.getName();

					// If the current level of Property is of type String
					if (method.getReturnType().equals(String.class)) {
						Object property = method.invoke(object);
						if (property != null) {
							Method setter = c.getMethod("set" + name.substring(3), new Class<?>[] { String.class });
							if (setter != null)
								// Setter to trim and set the trimmed String
								// value
								setter.invoke(object, String.valueOf(property).trim());
						}
					}
				}

			} catch (Exception e) {
				LOG.error("Strings cannot be trimmed because: ", e);
			}
		}

	}
	
	/**
	 * object 转map
	 * @param obj
	 * @return
	 */
	public static Map<String, Object> objectToMap(Object obj) {
		Map<String, Object> map = null;
        if(obj != null){    
        	try {
				map = new HashMap<String, Object>();   
				BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());    
				PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();    
				for (PropertyDescriptor property : propertyDescriptors) {    
				    String key = property.getName();    
				    if (key.compareToIgnoreCase("class") == 0) {   
				        continue;  
				    }  
				    Method getter = property.getReadMethod();  
				    Object value = getter!=null ? getter.invoke(obj) : null;  
				    map.put(key, value);  
				}
			} catch (Exception e) {
				LOG.error("",e);
			}
        }  
        return map;  
    }
	
	/**
	 * object 转 MultiValueMap
	 * @param obj
	 * @return
	 */
	public static MultiValueMap<String, Object> objectToMultiValueMap(Object obj){
		MultiValueMap<String, Object> map = null;
        if(obj != null){    
        	try {
				map = new LinkedMultiValueMap<String, Object>();   
				BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());    
				PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();    
				for (PropertyDescriptor property : propertyDescriptors) {    
				    String key = property.getName();    
				    if (key.compareToIgnoreCase("class") == 0) {   
				        continue;  
				    }  
				    Method getter = property.getReadMethod();  
				    Object value = getter!=null ? getter.invoke(obj) : null;  
				    map.add(key, value);  
				}
			} catch (Exception e) {
				LOG.error("",e);
			}
        }  
        return map;  
	}

	
}
